using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GameUI : MonoBehaviour
{
    private static GameUI m_instance;
    public static GameUI Instance { get => m_instance; }
    public Text Hp;

    public GameObject GamePass;
    public GameObject GameOver;


    void Start()
    {
        m_instance = this;

        GamePass.transform.Find("BtnRestart").GetComponent<Button>().onClick.AddListener(() =>
        {
            RestartGame();
        });

        GameOver.transform.Find("BtnRestart").GetComponent<Button>().onClick.AddListener(() =>
        {
            RestartGame();
        });

        UpdateHp();
        // 注册事件
        Global.HPChangedEvent += UpdateHp;
    }

    private void OnDestroy() {
        Global.HPChangedEvent -= UpdateHp;
    }

    void UpdateHp()
    {
        Hp.text = "HP:" + Global.HP;
    }

    void RestartGame()
    {
        Time.timeScale = 1;
        SceneManager.LoadScene("SampleScene");
        Global.ResetData();
    }
}
